package com.manor.paradise.dialog

import android.content.Context
import android.graphics.Color
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.bean.MineInfoBean
import com.manor.paradise.bean.MsgBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.manor.paradise.view.MineInfoListener
import kotlinx.android.synthetic.main.dialog_hire_elves_popup.view.*
import org.greenrobot.eventbus.EventBus
import java.math.BigDecimal

/**
 * 雇佣精灵
 */
class HireElvesPopup(var mineInfoBean: MineInfoBean, context: Context) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_hire_elves_popup
    }

    var type = true

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        tvDetail.setOnClickListener(this)
        tv_name.setText(mineInfoBean.name)
        tv_msg.setText("你将扣除" + mineInfoBean.price + "米粒以雇佣"+mineInfoBean.name)
        if (mineInfoBean.price.compareTo(BigDecimal("0")) == 0) {
            if (!SpUtil.getInstance().getBooleanValue(SpUtil.FRIRYLAND_BGETSPIRIT)) {
                ToastUtil.show("默认交易密码为：666666")
            }
        }

        ed_password.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_password.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        tv_ks.setOnClickListener(this)
        tv_sj.setOnClickListener(this)
        tv_lmjl.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
                R.id.tvDetail -> {
                    setPopDialog(mineInfoBean.id)
                    dismiss()
                }
                R.id.tv_ks -> {
                    if (!type) {
                        tv_ks.background = DrawableUtil.getGradientDraw(8, "#F3941C", "#FC9C68", 0)
                        tv_sj.background = DrawableUtil.getGradientDraw(8, "#4dF3941C", "#4dFC9C68", 0)
                        type = true
                        tv_msg.setText("你将扣除" + mineInfoBean.price + "米粒以雇佣"+mineInfoBean.name)
                    }
                }
                R.id.tv_sj -> {
                    if (type) {
                        tv_sj.background = DrawableUtil.getGradientDraw(8, "#F3941C", "#FC9C68", 0)
                        tv_ks.background = DrawableUtil.getGradientDraw(8, "#4dF3941C", "#4dFC9C68", 0)

                        type = false
                        tv_msg.setText("你将扣除" + mineInfoBean.price + "能量以雇佣"+mineInfoBean.name)
                    }
                }
                R.id.tv_lmjl -> {
                    if (!TextUtils.isEmpty(ed_password.text.toString()) && ed_password.text.toString().length == 6) {
                        if (mineInfoBean.price.compareTo(BigDecimal("0")) == 0) {
                            tv_lmjl.isEnabled = false
                            ElvesHttpUtil.getHireReward(ed_password.text.toString(), object : JHttpCallback() {
                                override fun onSuccess(code: Int, msg: String, info: String) {

                                    ToastUtil.show(msg)
                                    if (code == 200) {
                                        SpUtil.getInstance().setBooleanValue(SpUtil.FRIRYLAND_BGETSPIRIT, true)
                                        EventBus.getDefault().post(MsgBean(0));
                                        dismiss()
                                    }else{
                                        tv_lmjl?.isEnabled = true
                                    }

                                }

                                override fun onError() {
                                    super.onError()
                                    tv_lmjl?.isEnabled = true
                                }
                            })
                        } else {
                            tv_lmjl.isEnabled = false
                            ElvesHttpUtil.getHire(mineInfoBean.id, ed_password.text.toString(), type, object : JHttpCallback() {
                                override fun onSuccess(code: Int, msg: String, info: String) {
                                    ToastUtil.show(msg)

                                    if (code == 200) {
                                        EventBus.getDefault().post(MsgBean(1));
                                        dismiss()
                                    }else{
                                        tv_lmjl?.isEnabled = true
                                    }

                                }

                                override fun onError() {
                                    super.onError()
                                    tv_lmjl?.isEnabled = true
                                }
                            })
                        }
                    } else {
                        if (TextUtils.isEmpty(ed_password.text.toString())) {
                            ToastUtil.show("请输入交易密码")
                        }
                        if (ed_password.text.toString().length < 6) {
                            ToastUtil.show("请输入6位交易密码")
                        }
                    }
                }

            }
    }

    private fun setPopDialog(id: BigDecimal) {
        ElvesHttpUtil.getMineralInfo(id, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var mineInfoBean: MineInfoBean = GsonUtil.gsonToBean<MineInfoBean>(info, MineInfoBean::class.java)

                XPopup.Builder(context)
                        .hasShadowBg(true)
                        .dismissOnTouchOutside(true)
                        .hasStatusBarShadow(false) //启用状态栏阴影
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .asCustom(WizardDetailsPopup(mineInfoBean, context))
                        .show()
            }

            override fun onError() {
                super.onError()

            }
        })

    }

    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SPIRIT_HIRE)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_HIRE_REWARD)

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SPIRIT_INFO)
        super.onDestroy()
    }
}